//
// File  : CustomerDatabase.cpp
// Team  : Dangling Pointers
// Class : CST 236 - Software Testing
//
// CustomerDatabase definitions.
//

#include "CustomerDatabase.h"
#include <iostream>
#include <iomanip>


// constructor
CustomerDatabase::CustomerDatabase() : m_database()
{}

//
// getCustomer : const Customer&
//
// Method will retreive a specified customer from the database by key. If the
// specifed customer doesn't exist, method will throw an exception.
//
const Customer& CustomerDatabase::getCustomer(const std::string& key) const
{
	CustomerMapIterC iter = m_database.find(key);

	if (iter != m_database.end())
		return iter->second;

	throw std::exception();
}

//
// addCustomer : void
//
// method takes a string representing the key and a customer object to be
// added to the database.
//
void CustomerDatabase::addCustomer(const std::string& key, const Customer& customer)
{
	m_database.insert(std::make_pair(key, customer));
}

//
// changeCustomerPhone : void
//
// method takes a string representing the key and a string representing the
// phone number to be modified. If the key isn't found, an exception is thrown.
//
void CustomerDatabase::changeCustomerPhone(const std::string& key, const std::string& phone)
{
	CustomerMapIter iter = m_database.find(key);

	if (iter != m_database.end())
		iter->second.setPhoneNumber(phone);
	else 
		throw std::exception();
}

//
// changeCustomerEmail : void
//
// method takes a string representing the key and a string representing the
// email to be modified. If the key isn't found, an exception is thrown
//
void CustomerDatabase::changeCustomerEmail(const std::string& key, const std::string& email)
{
	CustomerMapIter iter = m_database.find(key);

	if(iter != m_database.end())
		iter->second.setEmail(email);
	else
		throw std::exception();
}

//
// updateCustomerMileage : void
//
// method takes a string representing the key and string representing the customer's
// mileage to be updated.
//
void CustomerDatabase::updateCustomerFlight(const std::string& key, const std::string& flight)
{
	CustomerMapIter iter = m_database.find(key);

	if(iter != m_database.end())
		iter->second.setFlight(flight);
	else
		throw std::exception();
}

//
// printCustomers : void
//
// method will print all of the customer database contents to the console
// in a formatted output.
//
void CustomerDatabase::printCustomers() const
{
	if(m_database.size() == 0)
	{
		std::cout << std::endl << "====== Zero Customers ======" 
			<< std::endl;
	}
	else
	{
		std::cout << std::endl << "======"
			<< m_database.size()
			<< " Customer(s) ======"
			<< std::endl;

		std::cout << std::left
			<< std::setw(20) << "Customer Name" 
			<< std::setw(15) << "Phone Number" 
			<< std::setw(25) << "Email" 
			<< "Flight" 
			<< std::endl;

		for (int i = 0; i < 71; ++i)
			std::cout << "-";
		std::cout << std::endl;

		for(CustomerMapIterC iter = m_database.begin(); iter != m_database.end(); ++iter)
			iter->second.print();
		std::cout << std::endl;
	}
}
